package edu.mtu.citizenscience.mushroommapper.model.family.genus;

import com.openswimsoftware.orm.model.Model;
import com.openswimsoftware.orm.model.annotation.Parent;
import com.openswimsoftware.orm.model.exception.InvalidModelField;
import com.openswimsoftware.orm.model.query.where.WhereOperator;

import android.content.Context;

import edu.mtu.citizenscience.mushroommapper.model.family.ModelGenus;
/**
 * @author Corbin Uselton <corbinu@cwuselto@mtu.edu>
 * @copyright (c) 2012 MichiganTech <http://www.mtu.edu>
 * @since 1.0
 * 
 * Model of a mushroom species in the app DB belongs to 
 * a genus has a name
 */
public class ModelSpecies extends Model<ModelSpecies> {
	// reference var for passing species model id between activities
	public static final String SPECIES_ID = "SPECIES_ID";
	
	// mushroom species name
	private String name;
	
	// mushroom species parent genus
	@Parent
	private ModelGenus genus;
	
	/**
	 * Create the species model
	 * 
	 * @param   context - Context - context for model operations
	 * @return	void
	 */
	public ModelSpecies(Context context) 
	{
		super(context, ModelSpecies.class);
	}
	
	/**
	 * Create the species model
	 * 
	 * @param   context - Context - context for model operations
	 * @param	id - long - the id of the model to load
	 * @return	void
	 */
	public ModelSpecies(Context context, long id) 
	{
		super(context, ModelSpecies.class, id);
	}
	
	/**
	 * Get the species name
	 * 
	 * @param   void
	 * @return	String - the genus name
	 */
	public String getName() 
	{
		// make sure the model is loaded
		this.load();
		
		return name;
	}
	
	/**
	 * Set the species name
	 * 
	 * @param   name - String - the species name
	 * @return	void
	 */
	public void setName(String name) 
	{
		// mark the model as updated
		this.updated = true;
		
		this.name = name;
	}
	
	public ModelGenus getGenus()
	{
		// make sure the model is loaded
		this.load();
		
		return this.genus;
	}
	
	public void setGenus(ModelGenus genus)
	{
		// mark the model as updated
		this.updated = true;
		
		this.genus = genus;
	}
	
	public ModelSpecies find(String name)
	{
		try
		{
			// return species model where name equals the passed name
			return this.findQuery().where("name", WhereOperator.EQUAL, name).execute();
		} 
		catch (InvalidModelField e) 
		{
			// happens if model is not valid
			e.printStackTrace();
		}
		
		// if a problem return nothing
		return null;
	}

}
